package steven.sdc.appPoker.Fragment;


import java.util.ArrayList;

import steven.sdc.appPoker.Adapter.AdapterNguoiChoi;
import steven.sdc.appPoker.Items.ItemNguoiChoi;

import com.amurani.slidingmenu.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentNguoiChoi extends Fragment {
	EditText edit_timkiem;
	ItemNguoiChoi dt=null;
	ArrayList<ItemNguoiChoi> items;
	AdapterNguoiChoi adapter;
	ListView list;
	final CharSequence[] diaglog = {"Xem chi tiết", "Xóa", "Hủy"};
	final CharSequence co = "Có";
	final CharSequence khong = "Không";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.them_nguoi_choi, null);
		list = (ListView) view.findViewById(R.id.list_nguoichoi);
		Init(view);
		
		
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		List();
		ListViewEvent();
		EditTextEvent();
		

	}
	
	
	public void EditTextEvent()
	{
		edit_timkiem.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				adapter.getFilter().filter(s.toString());
			
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	public void ListViewEvent()
	{
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1,  final int arg2,
					long arg3) {
				
				AlertDialog.Builder alert= new AlertDialog.Builder(getActivity());
				alert.setTitle("Chọn thao tác");
				alert.setItems(diaglog, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0){
							Fragment frag_chitiet = new FragmentChiTiet();
							getFragmentManager().beginTransaction().add(R.id.fragment_content, frag_chitiet,null).addToBackStack(null).commitAllowingStateLoss();
						}
						if(which == 1 ){
							AlertDialog.Builder ok = new AlertDialog.Builder(getActivity());
							ok.setTitle("Bạn có muốn xóa?");
							ok.setPositiveButton(co, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
										items.remove(arg2);
										adapter.notifyDataSetChanged();
										Toast.makeText(getActivity(), "Xóa thành công!!!", Toast.LENGTH_SHORT).show();
									
								}
							});
							ok.setNegativeButton(khong, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							});
							ok.show();
						}
						if(which == 2){
							
						}
					}
				});
				alert.show();	 
			}
		}) ;
		
	}
	public void List()
	{

		items.add(new ItemNguoiChoi(1, "Tiến", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(2, "Hoàng", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 11, 6, 17));
		items.add(new ItemNguoiChoi(3, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(4, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(5, "Tiến", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(6, "Hoàng", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 11, 6, 17));
		items.add(new ItemNguoiChoi(7, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(8, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(9, "Tiến", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(10, "Hoàng", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 11, 6, 17));
		items.add(new ItemNguoiChoi(11, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(12, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(13, "Tiến", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(14, "Hoàng", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 11, 6, 17));
		items.add(new ItemNguoiChoi(15, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(16, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(17, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(18, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(19, "Tiến", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(20, "Hoàng", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 11, 6, 17));
		items.add(new ItemNguoiChoi(21, "Bình", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		items.add(new ItemNguoiChoi(22, "Nhân", R.drawable.ic_matcuoi, R.drawable.ic_matmeo, 12, 8, 20));
		adapter.notifyDataSetChanged();
		
	}
	public void Init(View view)
	{items = new ArrayList<ItemNguoiChoi>();
		list = (ListView) view.findViewById(R.id.list_nguoichoi);
		edit_timkiem=(EditText)view.findViewById(R.id.editText_nguoichoi);
		adapter = new AdapterNguoiChoi(getActivity(),R.layout.list_item_nguoichoi1, items);
		list.setAdapter(adapter);
		
	}
}
